package com.qyhl.cloud.webtv.module_integral.message;

import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.MessageBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

public class MessageModel implements MessageContract.MessageModel {
    private MessagePresenter mPresenter;

    MessageModel(MessagePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getMessage(String username, String lastDate) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "msgHistory");
            object.put("username", username);
            object.put("lastDate", lastDate);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet( map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<List<MessageBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getMessageError(0, "网络异常！");
                    }

                    @Override
                    public void onSuccess(List<MessageBean> messageBeans) {
                        mPresenter.getMessageSuccess(messageBeans);
                    }
                });
    }

    @Override
    public void domark(String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "flagMsg");
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {

                    }
                });

    }
}
